<?php
declare (strict_types = 1);
/**
 * 日志
 */
namespace app\common\admin;

use app\common\facade\model\AdminModel;
use app\common\facade\model\AdminLogModel;
use app\common\facade\model\ConfigModel;

class Log extends BaseAdmin
{


  /**
   * 获取日志列表
   */
  public function LogList($param = [])
  {
    $whereLog = [];
    
    if (isset($param['query']) && !empty($param['query'])) $whereLog[] = ['msg', 'like', "%{$param['query']}%"];

    if (isset($param['types']) && !empty($param['types'])) $whereLog[] = ['type', '=' ,"{$param['types']}"];

    if (isset($param['user_id']) && !empty($param['user_id'])) $whereLog[] = ['user_id', '=' ,"{$param['user_id']}"];

    $LogList = AdminLogModel::LogList($whereLog, [], $param['offset'], $param['length']);

    return $LogList;
  }

    /*
    * 添加日志
    */
  public function AddLog($data)
  {
    return AdminLogModel::addSave($data);
  }

  /**
   * 获取当前用户日志
   */
  public function AuthUserLog($param)
  {
    $admininfo = self::adminLoginInfo();
    if (!isset($admininfo['id']) || empty($admininfo['id'])) exception('账号登录异常');

    $param['user_id'] = $admininfo['id']; 
    $list = self::LogList($param);

    return [
      'list' => $list['log'],
      'total' => $list['total']
    ];
  }

  public function getLogset()
  {
    $data['SysLog_days'] = ConfigModel::info(['name'=>'SysLog_days'])['value'];
    $data['SysLog_export_status'] = ConfigModel::info(['name'=>'SysLog_export_status'])['value'];
    return $data;
  }

}